package com.onjob.administer;

import java.text.MessageFormat;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.compass.core.support.search.CompassSearchCommand;
import org.compass.core.support.search.CompassSearchResults;

import util.compass.CompassSearchService;
import util.dao.Pagination;

import com.onjob.recruit.Company;
import com.onjob.recruit.CompanyDao;
import com.onjob.recruit.Recruit;
import com.onjob.recruit.RecruitDao;
import com.onjob.security.Function;
import com.onjob.security.FunctionDao;
import com.onjob.security.Groups;
import com.onjob.security.GroupsDao;
import com.onjob.security.Users;
import com.onjob.security.UsersDao;
import com.onjob.employment.PersonInfo;
import com.onjob.employment.PersonInfoDao;
import com.onjob.employment.Resume;
import com.onjob.employment.ResumeDao;
import com.onjob.photo.Photo;
import com.onjob.photo.PhotoDao;
import com.opensymphony.xwork2.ActionSupport;

/**
 * @author Administrator
 *
 */
public class AdministerAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String id;
	private Long userid;
	private Long functionid;
	private String keyword;
	private CompassSearchService compassSearchService;
	private CompassSearchResults results;
	
	private City city=new City();
	List<City> cityList;
	private CityDao cityDao;
	private Pagination<City> paginationCity=new Pagination<City>();
	
	private Status status=new Status();
	List<Status> statusList;
	private StatusDao statusDao;
	private Pagination<Status> paginationStatus=new Pagination<Status>();
	
	private Users users=new Users();
	private UsersDao usersDao;
	List<Users> usersList;
	Set<Groups> groupsSet;
	private Pagination<Users> paginationUsers=new Pagination<Users>();
	
	private Groups groups=new Groups();
	private GroupsDao groupsDao;
	List<Groups> groupsList;
	
	private Function function=new Function();
	private FunctionDao functionDao;
	List<Function> functionList;
	private Pagination<Function> paginationFunction=new Pagination<Function>();
	
	private Company company=new Company();
	private CompanyDao companyDao;
	List<Company> companyList;
	
	private Recruit recruit=new Recruit();
	private RecruitDao recruitDao;
	
	private PersonInfo personInfo;
	private PersonInfoDao personInfoDao;
	
	private Resume resume;
	private ResumeDao resumeDao;
	
	private Photo photo;
	private PhotoDao photoDao;
	
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	
	public Long getUserid() {
		return userid;
	}
	public void setUserid(Long userid) {
		this.userid = userid;
	}
	public Long getFunctionid() {
		return functionid;
	}
	public void setFunctionid(Long functionid) {
		this.functionid = functionid;
	}
	public String getKeyword() {
		return keyword;
	}
	public void setKeyword(String keyword) {
		this.keyword = keyword;
	}
	public CompassSearchService getCompassSearchService() {
		return compassSearchService;
	}
	public void setCompassSearchService(CompassSearchService compassSearchService) {
		this.compassSearchService = compassSearchService;
	}
	public CompassSearchResults getResults() {
		return results;
	}
	public void setResults(CompassSearchResults results) {
		this.results = results;
	}
	
	public City getCity() {
		return city;
	}
	public void setCity(City city) {
		this.city = city;
	}
	public CityDao getCityDao() {
		return cityDao;
	}
	public void setCityDao(CityDao cityDao) {
		this.cityDao = cityDao;
	}
	public List<City> getCityList() {
		return cityList;
	}
	public void setCityList(List<City> cityList) {
		this.cityList = cityList;
	}
	public Pagination<City> getPaginationCity() {
		return paginationCity;
	}
	public void setPaginationCity(Pagination<City> paginationCity) {
		this.paginationCity = paginationCity;
	}
	
	public Status getStatus() {
		return status;
	}
	public void setStatus(Status status) {
		this.status = status;
	}
	public StatusDao getStatusDao() {
		return statusDao;
	}
	public void setStatusDao(StatusDao statusDao) {
		this.statusDao = statusDao;
	}
	public List<Status> getStatusList() {
		return statusList;
	}
	public void setStatusList(List<Status> statusList) {
		this.statusList = statusList;
	}
	public Pagination<Status> getPaginationStatus() {
		return paginationStatus;
	}
	public void setPaginationStatus(Pagination<Status> paginationStatus) {
		this.paginationStatus = paginationStatus;
	}
	
	public Users getUsers() {
		return users;
	}
	public void setUsers(Users users) {
		this.users = users;
	}
	public UsersDao getUsersDao() {
		return usersDao;
	}
	public void setUsersDao(UsersDao usersDao) {
		this.usersDao = usersDao;
	}
	public List<Users> getUsersList() {
		return usersList;
	}
	public void setUsersList(List<Users> usersList) {
		this.usersList = usersList;
	}
	public Pagination<Users> getPaginationUsers() {
		return paginationUsers;
	}
	public void setPaginationUsers(Pagination<Users> paginationUsers) {
		this.paginationUsers = paginationUsers;
	}
	
	public Groups getGroups() {
		return groups;
	}
	public void setGroups(Groups groups) {
		this.groups = groups;
	}
	public GroupsDao getGroupsDao() {
		return groupsDao;
	}
	public void setGroupsDao(GroupsDao groupsDao) {
		this.groupsDao = groupsDao;
	}
	public List<Groups> getGroupsList() {
		groupsList=groupsDao.findAll();
		return groupsList;
	}
	public void setGroupsList(List<Groups> groupsList) {
		this.groupsList = groupsList;
	}
	public Set<Groups> getGroupsSet() {
		return groupsSet;
	}
	public void setGroupsSet(Set<Groups> groupsSet) {
		this.groupsSet = groupsSet;
	}
	
	public Function getFunction() {
		return function;
	}
	public void setFunction(Function function) {
		this.function = function;
	}
	public FunctionDao getFunctionDao() {
		return functionDao;
	}
	public void setFunctionDao(FunctionDao functionDao) {
		this.functionDao = functionDao;
	}
	public List<Function> getFunctionList() {
		return functionList;
	}
	public void setFunctionList(List<Function> functionList) {
		this.functionList = functionList;
	}
	public Pagination<Function> getPaginationFunction() {
		return paginationFunction;
	}
	public void setPaginationFunction(Pagination<Function> paginationFunction) {
		this.paginationFunction = paginationFunction;
	}
	
	public Company getCompany() {
		return company;
	}
	public void setCompany(Company company) {
		this.company = company;
	}
	public CompanyDao getCompanyDao() {
		return companyDao;
	}
	public void setCompanyDao(CompanyDao companyDao) {
		this.companyDao = companyDao;
	}
	public List<Company> getCompanyList() {
		return companyList;
	}
	public void setCompanyList(List<Company> companyList) {
		this.companyList = companyList;
	}
	
	public Recruit getRecruit() {
		return recruit;
	}
	public void setRecruit(Recruit recruit) {
		this.recruit = recruit;
	}
	public RecruitDao getRecruitDao() {
		return recruitDao;
	}
	public void setRecruitDao(RecruitDao recruitDao) {
		this.recruitDao = recruitDao;
	}
	
	public PersonInfo getPersonInfo() {
		return personInfo;
	}
	public void setPersonInfo(PersonInfo personInfo) {
		this.personInfo = personInfo;
	}
	public PersonInfoDao getPersonInfoDao() {
		return personInfoDao;
	}
	public void setPersonInfoDao(PersonInfoDao personInfoDao) {
		this.personInfoDao = personInfoDao;
	}
	
	public Resume getResume() {
		return resume;
	}
	public void setResume(Resume resume) {
		this.resume = resume;
	}
	public ResumeDao getResumeDao() {
		return resumeDao;
	}
	public void setResumeDao(ResumeDao resumeDao) {
		this.resumeDao = resumeDao;
	}
	
	public Photo getPhoto() {
		return photo;
	}
	public void setPhoto(Photo photo) {
		this.photo = photo;
	}
	public PhotoDao getPhotoDao() {
		return photoDao;
	}
	public void setPhotoDao(PhotoDao photoDao) {
		this.photoDao = photoDao;
	}
	
	public String createcity(){
		
		if(city.getId().isEmpty()){
			System.out.println("创建城市");
				cityDao.create(city);
		}else{
			System.out.println("xiugai城市");
			cityDao.update(city);
		}
	return SUCCESS;
		
	}
	public void validateCreatecity() {
		if(city.getId()==null){
			if (!cityDao.isUniqueName(city.getCityName())) {
				this.addFieldError("city.cityName", "该城市已存在");
			}
		}
		
	}
	public String updatecity() throws Exception{
		city=cityDao.findById(id);
		if(city==null){
			throw new RuntimeException(MessageFormat.format(
					"想修改的信息id({0})不存在", id));
		}
		return SUCCESS;
		
	}
	public String deletecity(){
		city=cityDao.findById(id);
		cityDao.delete(city);
		cityList=cityDao.findAll();
		return SUCCESS;
		
	}
	public String showcity(){
		if(this.paginationCity.getSize()==0){
			this.paginationCity.setSize(3);
		}
		if(this.paginationCity.getNo()<=0){
			this.paginationCity.setNo(1);
		}
		else{
			this.paginationCity.setNo(this.paginationCity.getNo());
		}
		cityDao.browse(paginationCity);
		cityList=paginationCity.getResults();
		return SUCCESS;
		
	}
	public String createstatus(){
		if(status.getId().isEmpty()){
			System.out.println("创status");
			statusDao.create(status);
		}else{
			System.out.println("change status");
			statusDao.update(status);
		}
		return SUCCESS;
	}
	public void validateCreatestatus() {
		if(status.getId()==null){
			if (!statusDao.isUniqueName(status.getStatusName())) {
				this.addFieldError("status.statusName", "该status已存在");
			}
		}
		
	}
	public String updatestatus(){
		status=statusDao.findById(id);
		if(status==null){
			throw new RuntimeException(MessageFormat.format(
					"想修改的信息id({0})不存在", id));
		}
		return SUCCESS;
		
	}
	public String deletestatus(){
		status=statusDao.findById(id);
		statusDao.delete(status);
		statusList=statusDao.findAll();
		return SUCCESS;
		
	}
	public String showstatus(){
		if(this.paginationStatus.getSize()==0){
			this.paginationStatus.setSize(3);
		}
		if(this.paginationStatus.getNo()<=0){
			this.paginationStatus.setNo(1);
		}
		else{
			this.paginationStatus.setNo(this.paginationStatus.getNo());
		}
		statusDao.browse(paginationStatus);
		statusList=paginationStatus.getResults();
		return SUCCESS;
		
	}
	public String createusers(){
		if(users.getId()==null){
			System.out.println("+++++++++add user+++++++++++");
			groups=groupsDao.findById(groups.getId());
			groupsSet=new HashSet<Groups>();
			groupsSet.add(groups);
			users.setGroupsId(groupsSet);
			System.out.println(groupsSet.iterator().next().getGroupName());
			usersDao.create(users);
		}else{
			System.out.println("++++change user++++++");
			groups=groupsDao.findById(groups.getId());
			groupsSet=new HashSet<Groups>();
			groupsSet.add(groups);
			users.setGroupsId(groupsSet);
			usersDao.update(users);
		}
		return SUCCESS;
	}
	public void validateCreateusers() {
		if(users.getId()==null){
			if (!usersDao.isUniqueName(users.getUserEmail())) {
				this.addFieldError("users.userName", "该user已存在");
			}
		}
	}
	public String deleteusers(){
		this.users=usersDao.findById(userid);
		System.out.println("<<<<<<<<<<<<<<<<"+this.personInfoDao+">>>>>>>>");
		if(this.users.getGroupsId().iterator().next().getId()==2){
			if(null != personInfoDao.findByType(userid.toString(),"userId.id")){
				this.personInfo =  this.personInfoDao.findByType(userid.toString(),"userId.id");
				if(null != this.resumeDao.findResumeByType(this.personInfo.getId(), "personInfo.id")){
					this.resume  = this.resumeDao.findResumeByType(this.personInfo.getId(), "personInfo.id");
					if(null != this.photoDao.findPhotoByType(this.resume.getId(), "resumeId.id")){
						this.photoDao.delete(this.photoDao.findPhotoByType(this.resume.getId(), "resumeId.id"));
						this.resumeDao.delete(this.resume);
						this.personInfoDao.delete(this.personInfo);
					}else{
						this.resumeDao.delete(this.resume);
						this.personInfoDao.delete(this.personInfo);
					}
				}else{
					this.personInfoDao.delete(this.personInfo);
				}
			}
		}
		if(users.getGroupsId().iterator().next().getId()==1){
			company=companyDao.getCompanyByUser(users);
			if(recruitDao.isCompanyHaveRecruit(company)){
				recruitDao.deleteRecruitFindByCompany(company);
				companyDao.deleteCompanyFindByUser(userid);
			}else{
				companyDao.deleteCompanyFindByUser(userid);
			}
		}
		usersDao.delete(users);
		return SUCCESS;
	}
	public String updateusers(){
		users=usersDao.findById(userid);
		System.out.println(users);
		if(users==null){
			throw new RuntimeException(MessageFormat.format(
					"想修改的信息id({0})不存在", userid));
		}
		return SUCCESS;
		
	}
	public String showusers() throws Exception{
		if(this.paginationUsers.getSize()==0){
			this.paginationUsers.setSize(3);
		}
		if(this.paginationUsers.getNo()<=0){
			this.paginationUsers.setNo(1);
		}
		else{
			this.paginationUsers.setNo(this.paginationUsers.getNo());
		}
		usersDao.browse(paginationUsers);
		usersList=paginationUsers.getResults();
		return SUCCESS;
		
	}
	
	public String createfunction(){
		if(function.getId()==null){
			System.out.println("+++++++++add function+++++++++++");
			groups=groupsDao.findById(groups.getId());
			groupsSet=new HashSet<Groups>();
			groupsSet.add(groups);
			function.setGroupsId(groupsSet);
			System.out.println(groupsSet.iterator().next().getGroupName());
			functionDao.create(function);
		}else{
			System.out.println("++++change function++++++");
			groups=groupsDao.findById(groups.getId());
			groupsSet=new HashSet<Groups>();
			groupsSet.add(groups);
			function.setGroupsId(groupsSet);
			functionDao.update(function);
		}
		return SUCCESS;
	}
	public String deletefunction(){
		function=functionDao.findById(functionid);
		functionDao.delete(function);
		return SUCCESS;
	}
	public String updatefunction(){
		function=functionDao.findById(functionid);
		if(function==null){
			throw new RuntimeException(MessageFormat.format(
					"想修改的信息id({0})不存在", functionid));
		}
		return SUCCESS;
		
	}
	public String showfunction() throws Exception{
		if(this.paginationFunction.getSize()==0){
			this.paginationFunction.setSize(3);
		}
		if(this.paginationFunction.getNo()<=0){
			this.paginationFunction.setNo(1);
		}
		else{
			this.paginationFunction.setNo(this.paginationFunction.getNo());
		}
		functionDao.browse(paginationFunction);
		functionList=paginationFunction.getResults();
		return SUCCESS;
		
	}
	
	public String search() throws Exception{
		try{
			results = compassSearchService.search(new CompassSearchCommand(keyword, 0));
			for(int i=0;i<results.getHits().length;i++){
						city=(City)results.getHits()[i].getData();
				System.out.println(results.getPages().length+"++++++++++"+city.getCityName());
			}
			
			
		}catch(Exception e){
			throw new Exception(e);
		}
		return SUCCESS;
	} 
	
	public String execute() throws Exception{
		
		return SUCCESS;
	}
	public String input(){
		
		return "input";
	}
}
